package com.itheima.table03;

import com.itheima.bean.Course;
import com.itheima.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Test03 {

    @Test
    public void selectAll() throws Exception {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession(true);
        Many_To_Many mapper = sqlSession.getMapper(Many_To_Many.class);
        List<Student> students = mapper.selectAll();
        for (Student student : students) {
            System.out.println(student.getId()+","+student.getName()+","+student.getAge());
            List<Course> courses = student.getCourses();
            for (Course course : courses) {
                System.out.println("\t"+course);
            }
        }
        is.close();
        sqlSession.close();

    }
}
